// Town 14: Western Bahssikava
begintownscript;

variables;

short choice,dummy,i,j,k,access,authoriz,security,power,safety;
string dlgstr;

body;

beginstate INIT_STATE;
turn_off_training(1);

if (get_flag(214,0) == 1)
	erase_char(37);

if (get_flag(14,29) > 0)
	{erase_char(16);
	erase_char(15);
	erase_char(14); }

if (get_flag(214,0) > 0)
	{erase_char(37);
	erase_char(74);
	erase_char(75);
	erase_char(76);
	erase_char(77);
	erase_char(78);
	erase_char(79);
	erase_char(80);
	erase_char(84);
	}

enable_add_chars(1);
set_level(73,60);
set_name(73,"Large Haakai");
set_level(37,45);
set_name(37,"Boarag");

char_give_item(21,134); // Blessed Large Shield
char_give_item(47,465); // Pants of Health

if (get_flag(14,0) == 0) {
	set_flag(14,16,1);
	set_flag(14,17,1);
	set_flag(14,18,3);
	set_flag(14,19,2);
	set_flag(14,20,1);
	force_instant_terrain_redraw();
	message_dialog("You reach the other side of the passage, and you can hear many demons growling. The sounds of terrible spells and screams echo down these narrow cave hallways.","The place where you are is VERY dangerous. It would be best to watch your step.");
	set_flag(14,0,1);
	i = 3;
	if (character_in_party(906) > 0)
		i = character_in_party(906);
	else 
		while (char_ok(i) == 0)
			{i = i - 1; }
		
	text_bubble_on_char(i,"This place is creepy.");
	force_instant_terrain_redraw();
	pause(6 * get_flag(250,8));
	
	if (party_size() == 1)
		end();
		
	j = 0;
	while (char_ok(j) == 0)
		{j = j + 1; }
	text_bubble_on_char(i,"");
	text_bubble_on_char(j,"No kidding.");
	force_instant_terrain_redraw();
	pause(6 * get_flag(250,8));
	}
	
	access = get_flag(14,16);
	authoriz = get_flag(14,17);
	security = get_flag(14,18);
	power = get_flag(14,19);
	safety = get_flag(14,20);
	
break;

beginstate EXIT_STATE;
	set_flag(14,16,access);
	set_flag(14,17,authoriz);
	set_flag(14,18,security);
	set_flag(14,19,power);
	set_flag(14,20,safety);
break;

beginstate START_STATE;
i = get_ran(1,1,100);
if (i > 5)
	end();
if (i == 1)
	if (get_flag(14,21) == 0) // haven't freed trapped ghosts in the crystal
		play_sound(-111); // ghost scream
if (i == 2)
	if (get_flag(14,29) == 0) // haven't freed living sliths
		play_sound(-46); // dark curse
if (i == 3)
	if (get_flag(214,0) == 0) // haven't free imps
		play_sound(-65); // drain blast
if (i == 4)
	play_sound(-51); // flames blast
if (i == 5)
	if (get_flag(14,11) == 0) // haven't destroyed sw crystal power N-K
		play_sound(-54); // lightning zap
break;

beginstate 10;
if ((get_flag(250,4) == 1) && (has_item(451) == 0))
	{message_dialog("You can't just leave Phaedra behind. You must take her with you to the guardpost immediately or she will die.","");
	block_entry(1);
	end();
	}

	reset_dialog();
	add_dialog_str(0,"There is a small passage here. Do you wish to walk down it?",0);
	add_dialog_choice(0,"Leave.");
	add_dialog_choice(1,"Walk down the passage.");
	choice = run_dialog(1);
	if (choice == 1)
		block_entry(1);
	if (choice == 2)
		move_to_new_town(13,6,18);
break;

beginstate 11;
if (get_flag(14,1) != 0)
	end();

	message_dialog("You hear many loud, demonic sounds coming from this direction. Proceeding this way would probably be suicide.","");
	set_flag(14,1,1);
break;

beginstate 12;
if (get_flag(14,2) != 0)
	end();

	message_dialog("You step into what is clearly a prison and torture chamber. But in the cells, you see living sliths, not ghosts!","This bears further investigation, but at the moment, there are demons to fight.");
	set_flag(14,2,1);
break;

beginstate 13;
if (get_flag(14,6) != 0)
	end();

	message_dialog("You open these doors and see a bar-room brawl in progress! The demons apparently do not get along tremendously well with each other. However, neither side appears to like you very much, either.","");
	set_flag(14,6,1);
break;

beginstate 14;
if (get_flag(14,7) != 0)
	end();

	message_dialog("The room you are leaving, you notice, is constructed in the Bahssikavan style that by now is somewhat familiar to you. However, it was taken over by demons, and they rearranged everything to the point that you could not discern its original purpose.","This pattern seems likely to repeat itself with the rest of the rooms you may find near here. Whatever they are now does not necessarily have anything to do with what they were originally.");
	set_flag(14,7,1);
break;

beginstate 15;
	if (has_special_item(4) < 1)
		{message_dialog("This wheel is locked, and you don't have the key. You eye the chains, but you are pretty sure that they are too strong for you to break using just your weapons. You can't turn this wheel.","");
		end(); }
		
	reset_dialog();
	add_dialog_str(0,"This wheel is locked, but the key that you found in the chest in the large haakai's room unlocks it. Do you wish to turn it?",0);
	add_dialog_choice(0,"Leave.");
	add_dialog_choice(1,"Turn the wheel.");
	choice = run_dialog(1);
	if ((choice == 1) || (get_flag(14,29) != 0))
		end();
	
	set_terrain(49,59,68);
	set_terrain(52,59,68);
	set_terrain(55,59,68);
	set_terrain(58,59,68);
	set_terrain(61,59,68);
	force_instant_terrain_redraw();
	play_sound(99);
	
	relocate_character(16,49,59);
	set_character_facing(16,0);
	force_instant_terrain_redraw();
	pause(1 * get_flag(250,8));
	
	relocate_character(16,50,58);
	set_character_facing(16,7);
	force_instant_terrain_redraw();
	pause(1 * get_flag(250,8));
	
	relocate_character(16,51,58);
	set_character_facing(16,6);
	force_instant_terrain_redraw();
	pause(1 * get_flag(250,8));
	
	relocate_character(16,52,57);
	set_character_facing(16,7);
	force_instant_terrain_redraw();
	pause(1 * get_flag(250,8));
	
	relocate_character(16,53,57);
	set_character_facing(16,7);
	force_instant_terrain_redraw();
	pause(1 * get_flag(250,8));
	
	relocate_character(16,54,57);
	force_instant_terrain_redraw();
	pause(1 * get_flag(250,8));
	
	relocate_character(16,55,57);
	force_instant_terrain_redraw();
	pause(1 * get_flag(250,8));
	
	relocate_character(16,56,57);
	force_instant_terrain_redraw();
	pause(1 * get_flag(250,8));
	
	if (get_flag(250,2) < 2)
		{reset_dialog();
		if (get_flag(250,2) == 0)
			add_dialog_str(0,"The newly freed, living slith approaches you and begins speaking. Unfortunately, you don't understand a word that he is saying. You try to respond back in your native language, but he doesn't understand you.",0);
		else
			{add_dialog_str(0,"The newly freed, living slith approaches you and begins speaking. He is speaking Bahssikavan! You can almost understand him. He is thanking you, and saying something about defeating Nolagh-Khar, probably that he will help you in some way.",0);
			add_dialog_str(1,"You try to respond back, first in his language, and then, failing that, in your native language, but he doesn't understand you.",0);
			}
		add_dialog_str(2,"Clearly frustrated, he approaches you, grabs your hand, and squeezes it firmly. He signals to his compatriots in the other cells, who emerge, and the three of them leave the room.",0);
		add_dialog_str(3,"You have clearly done something good here, but you can't be completely sure what it is.",0);
		set_flag(14,29,run_dialog(1));
		erase_char(16);
		erase_char(15);
		erase_char(14);
		end();
		}
		
	message_dialog("The newly freed, living slith approaches you and begins speaking. He is speaking Bahssikavan! You can understand him, too, although just barely.","");
		
	i = 0;
	while (char_ok(i) == 0)
		{i = i + 1; }
		
	force_view_center(59,56);
	force_instant_terrain_redraw();
		
	text_bubble_on_char(16,"You have freed us!");
	force_instant_terrain_redraw();
	pause(6 * get_flag(250,8));
	
	text_bubble_on_char(16,"");
	text_bubble_on_char(16,"Are you here to fight Nolagh-Khar?");
	force_instant_terrain_redraw();
	pause(6 * get_flag(250,8));
	
	text_bubble_on_char(16,"");
	if (party_size() == 1)
		text_bubble_on_char(i,"Yes, I am.");
	else
		text_bubble_on_char(i,"Yes, we are.");
	force_instant_terrain_redraw();
	pause(6 * get_flag(250,8));
	
	text_bubble_on_char(i,"");
	text_bubble_on_char(16,"That is good.");
	force_instant_terrain_redraw();
	pause(6 * get_flag(250,8));
	
	text_bubble_on_char(16,"");
	text_bubble_on_char(16,"I can help you.");
	force_instant_terrain_redraw();
	pause(6 * get_flag(250,8));
	
	text_bubble_on_char(16,"");
	text_bubble_on_char(16,"I am from a town nearby.");
	force_instant_terrain_redraw();
	pause(6 * get_flag(250,8));
	
	text_bubble_on_char(16,"");
	text_bubble_on_char(16,"There is no time to hear");
	force_instant_terrain_redraw();
	pause(6 * get_flag(250,8));
	
	text_bubble_on_char(16,"");
	text_bubble_on_char(16,"the story of how I came to Bahssikava");
	force_instant_terrain_redraw();
	pause(6 * get_flag(250,8));
	
	text_bubble_on_char(16,"");
	text_bubble_on_char(16,"and how the demons captured me.");
	force_instant_terrain_redraw();
	pause(6 * get_flag(250,8));
	
	text_bubble_on_char(16,"");
	text_bubble_on_char(16,"We must move.");
	force_instant_terrain_redraw();
	pause(6 * get_flag(250,8));
	
	text_bubble_on_char(16,"");
	text_bubble_on_char(16,"We must free the Bahssikavans");
	force_instant_terrain_redraw();
	pause(6 * get_flag(250,8));
	
	text_bubble_on_char(16,"");
	text_bubble_on_char(16,"from their demon slavemasters.");
	force_instant_terrain_redraw();
	pause(6 * get_flag(250,8));
	
	text_bubble_on_char(16,"");
	text_bubble_on_char(16,"Will you allow me to help you?");
	force_instant_terrain_redraw();
	pause(6 * get_flag(250,8));
	
	text_bubble_on_char(16,"");
	force_instant_terrain_redraw();
	pause(1 * get_flag(250,8));
	
	reset_dialog();
	add_dialog_str(0,"What do you say?",0);
	add_dialog_choice(0,"Yes, you can come with us.");
	add_dialog_choice(1,"No, we'd rather not have your help.");
	choice = run_dialog(1);
	if (choice == 2)
		{set_flag(14,29,1);
		
		text_bubble_on_char(16,"Very well.");
		force_instant_terrain_redraw();
		pause(6 * get_flag(250,8));
		
		text_bubble_on_char(16,"");
		text_bubble_on_char(16,"I will be present");
		force_instant_terrain_redraw();
		pause(6 * get_flag(250,8));
		
		text_bubble_on_char(16,"");
		text_bubble_on_char(16,"if you combat Nolagh-Khar");
		force_instant_terrain_redraw();
		pause(6 * get_flag(250,8));
		
		text_bubble_on_char(16,"");
		text_bubble_on_char(16,"but I will not burden you otherwise.");
		force_instant_terrain_redraw();
		pause(6 * get_flag(250,8));
		
		text_bubble_on_char(16,"");
		text_bubble_on_char(16,"I thank you again.");
		force_instant_terrain_redraw();
		pause(6 * get_flag(250,8));
		
		text_bubble_on_char(16,"");
		text_bubble_on_char(16,"Farewell.");
		force_instant_terrain_redraw();
		pause(6 * get_flag(250,8));
		
		erase_char(16);
		erase_char(15);
		erase_char(14);
		
		force_view_center(char_loc_x(i),char_loc_y(i));
		force_instant_terrain_redraw();
		
		end();
		}
	
	text_bubble_on_char(16,"Good.");
	force_instant_terrain_redraw();
	pause(6 * get_flag(250,8));
	
	text_bubble_on_char(16,"");
	text_bubble_on_char(16,"I know much that will be useful.");
	force_instant_terrain_redraw();
	pause(6 * get_flag(250,8));
	
	text_bubble_on_char(16,"");
	text_bubble_on_char(16,"Nolagh-Khar is far to the north,");
	force_instant_terrain_redraw();
	pause(6 * get_flag(250,8));
	
	text_bubble_on_char(16,"");
	text_bubble_on_char(16,"But before you fight him,");
	force_instant_terrain_redraw();
	pause(6 * get_flag(250,8));
	
	text_bubble_on_char(16,"");
	text_bubble_on_char(16,"you will need to disrupt");
	force_instant_terrain_redraw();
	pause(6 * get_flag(250,8));
	
	text_bubble_on_char(16,"");
	text_bubble_on_char(16,"his power supply.");
	force_instant_terrain_redraw();
	pause(6 * get_flag(250,8));
	
	text_bubble_on_char(16,"");
	text_bubble_on_char(16,"If you don't, he will kill you.");
	force_instant_terrain_redraw();
	pause(6 * get_flag(250,8));
	
	text_bubble_on_char(16,"");
	text_bubble_on_char(16,"One of the weak points in the system");
	force_instant_terrain_redraw();
	pause(6 * get_flag(250,8));
	
	text_bubble_on_char(16,"");
	text_bubble_on_char(16,"is to the west of here.");
	force_instant_terrain_redraw();
	pause(6 * get_flag(250,8));
	
	text_bubble_on_char(16,"");
	text_bubble_on_char(16,"I do not know of others,");
	force_instant_terrain_redraw();
	pause(6 * get_flag(250,8));
	
	text_bubble_on_char(16,"");
	text_bubble_on_char(16,"but I am sure there are some.");
	force_instant_terrain_redraw();
	pause(6 * get_flag(250,8));
	
	text_bubble_on_char(16,"");
	text_bubble_on_char(16,"Come, let us move.");
	force_instant_terrain_redraw();
	pause(6 * get_flag(250,8));
	
	text_bubble_on_char(16,"");
	text_bubble_on_char(16,"We should work quickly");
	force_instant_terrain_redraw();
	pause(6 * get_flag(250,8));
	
	text_bubble_on_char(16,"");
	text_bubble_on_char(16,"to defeat Nolagh-Khar.");
	force_instant_terrain_redraw();
	pause(6 * get_flag(250,8));
	
	i = add_char_to_party(16);
	if (i == 0)
		{message_dialog("Somehow you do not have room in your party for Ithik. Remove characters so as to make room for him.","");
		erase_char(16); }
		
	erase_char(15);
	erase_char(14);
	
	force_view_center(char_loc_x(i),char_loc_y(i));
	force_instant_terrain_redraw();
	
	set_flag(14,29,1);
break;

beginstate 16;
if (get_flag(14,8) != 0)
	end();

	message_dialog("The moment you open this door, a hot burst of air rushes into your face. You hear the sounds of metal creaking and diabolical growls, and your first sight confirms what your other senses have told you: demons and machinery are behind this door.","");
	set_flag(14,8,1);
break;

beginstate 17;
	reset_dialog();
	add_dialog_str(0,"The infernal demon lord dies, screaming and gushing flames everywhere.",0);
	if ((char_ok(26)) || (char_ok(27)) || (char_ok(28)) || (char_ok(29)))
		add_dialog_str(1,"Now you only have to worry about the other demons nearby.",0);
	dummy = run_dialog(1);
	put_boom_on_char(25,0,1);
	put_boom_on_char(25,0,1);
	put_boom_on_char(25,0,1);
	put_boom_on_char(25,0,1);
	put_boom_on_char(25,0,1);
	put_boom_on_char(25,0,1);
	run_animation_sound(167);
	damage_near_loc(char_loc_x(25),char_loc_y(25),get_ran(15,1,10),5,1); 
break;

beginstate 18;
	if ((get_flag(14,9) != 0) || (is_combat()))
	end();

	message_dialog("All of the machinery around here is quite elaborate, and it reminds you a great deal of the machinery you saw underneath the Bahssikavan catacombs. Probably the Bahssikavans built it, but the demons have taken it for their own use.","That use seems to be to power this crystal in the corner. Whatever it is, it is absorbing a HUGE amount of energy.");
	if (character_in_party(1410) > 0)
		message_dialog("_I know this,_ Ithik says suddenly. _It is one of the crystals that give Nolagh-Khar his power! We must destroy it._","");
	set_flag(14,9,1);
break;

beginstate 19;
if (get_flag(14,10) != 0)
	end();
	
	reset_dialog();
	add_dialog_str(0,"In front of you is a strange crystal, one that is absorbing huge amounts of energy from the surrounding machinery. It glows a deep blue, and it heats the room like a furnace. Between you and it are some complex controls.",0);
	if (get_flag(250,2) == 0)
		add_dialog_str(1,"The controls are covered with strange writing that you can't decipher. Even despite your lack of understanding, it is still very tempting to muck around with them. Do you?",0);
	if (get_flag(250,2) >= 1)
		add_dialog_str(1,"The controls are covered with Bahssikavan writing. It is complicated, but you think you can understand some of it. It has something to do with changing the energy flow to the crystal.",0);
	if (character_in_party(1410) > 0)
		add_dialog_str(2,"_This! I know this!_ Ithik bursts out. _You must use these controls to harm the demon. I will guide you._",0);
	add_dialog_choice(0,"Leave.");
	if (get_flag(250,2) < 1)
		add_dialog_choice(1,"Fool around with the controls.");
	if ((get_flag(250,2) >= 1) && (character_in_party(1410) < 4))
		add_dialog_choice(1,"Attempt to use the controls.");
	if ((get_flag(250,2) >= 1) && (character_in_party(1410) >= 4))
		add_dialog_choice(1,"Attempt to use the controls, following Ithik's instructions.");
	choice = run_dialog(1);
	if (choice == 1)
		end();
	if (choice == 2)
		{set_flag(14,10,1);
		if (get_flag(250,2) >= 1)
			{reset_dialog();
			if (character_in_party(1410) < 4)
				add_dialog_str(0,"You struggle with the controls, examining each lever and switch very carefully and painstakingly deciphering its label before deciding whether to pull or push it.",0);
			else
				add_dialog_str(0,"Ithik scans over the labels on the control panel and begins issuing instructions. You obey, pulling a multitude of levers and flipping a variety of switches.",0);
			add_dialog_str(1,"Eventually, the device emits a low hum, and all the machinery around you slows to a stop. You may have just done something very important.",0);
			set_flag(14,11,run_dialog(1));
			end();
			}
		if (get_flag(250,2) < 1)
			{message_dialog("You push buttons here and there, pulling levers, and generally having a good time. Everything seems fine until the room suddenly grows even hotter than before!","");
			put_boom_on_space(2,54,0,2);
			put_boom_on_space(2,54,0,2);
			put_boom_on_space(2,54,0,2);
			run_animation_sound(167);
			damage_char(1000,250,1);
			message_dialog("As the explosions envelope you, you begin to realize that you may have done something very bad.","");
			put_boom_on_space(2,54,0,2);
			put_boom_on_space(2,54,0,2);
			put_boom_on_space(2,54,0,2);
			run_animation_sound(167);
			damage_char(1000,250,1);
			}
		}
break;

beginstate 20;
if (get_flag(14,13) != 0)
	end();
	
	message_dialog("There is a boat here. It has a few crystal shards in it. It seems to be for transporting crystals up the river from -- you guess from the sounds you are hearing downriver -- a mine.","You can hear the sound of dozens of demons just a short ways away. It would be a bad idea to make ANY sort of noise, and paddling this boat would make noise. You can't get in the boat.");
	set_flag(14,13,1);
break;

beginstate 21;
if (get_flag(14,14) != 0)
	end();
	
	message_dialog("By appearances, this was once a higher-income Bahssikavan house. Elaborate molding, immaculate architecture, and gorgeous paintings attest this fact. However, in the years since it has been taken over by demons, it could have been put to any use.","You can feel raw energy in the air here. Some powerful magic (likely augmented by Bahssikavan machinery) is going on inside.");
	set_flag(14,14,1);
break;

beginstate 22; // SAFETY state
// **** Crystal Puzzle Notes ****
// ACCESS is state 27 and flag (14,16)
// AUTHORIZATION is state 26 and flag (14,17)
// SECURITY is state 25 and flag (14,18)
// POWER is state 24 and flag (14,19)
// SAFETY is state 22 and flag (14,20)
// These flags are set in the EXIT_STATE and checked in the INIT_STATE.
//
// Initial condition: low ACCESS, low AUTHORIZATION, high SECURITY, low POWER,
//	and low SAFETY.
// The solution is high ACCESS and medium POWER.
// Death comes from high ACCESS and high POWER.
//
// Limits:
//	Medium ACCESS requires medium or low SAFETY
//	High ACCESS requires medium or high AUTHORIZATION and low SAFETY
//
//	Medium AUTHORIZATION requires low SECURITY
//	High AUTHORIZATION is impossible
//
//	Low SECURITY requires medium or less SAFETY
//
//	Medium POWER requires medium or high AUTHORIZATION
//	High POWER requires medium or high AUTHORIZATION and low SAFETY
//
//	Low SAFETY requires medium or high AUTHORIZATION

	// Is puzzle active?
	if (get_flag(14,21))
		{message_dialog("You've fiddled with the control panels enough. You leave this one alone.","");
		end();
		}

	// Do they want to move the switch?
	reset_dialog();
	add_dialog_str(0,"You come to a control panel. It has a label and a switch with three settings.",0);
	if (get_flag(250,2) == 0)
		add_dialog_str(1,"The label has strange writing that you can't read, and above each of the three switches are more words that you can't read.",0);
	if (get_flag(250,2) == 1)
		add_dialog_str(1,"You can't read the label. Above each of the three settings are, from right to left, _LOW,_ _MEDIUM,_ and _HIGH._",0);
	if (get_flag(250,2) == 2)
		add_dialog_str(1,"The label says _SAFETY OVERRIDE._ Above each of the three settings are, from left to right, _LOW,_ _MEDIUM,_ and _HIGH._",0);
	if (safety == 1)
		add_dialog_str(2,"The switch is in the left position.",0);
	if (safety == 2)
		add_dialog_str(2,"The switch is in the center position.",0);
	if (safety == 3)
		add_dialog_str(2,"The switch is in the right position.",0);
	add_dialog_str(3,"What do you do?",0);
	add_dialog_choice(0,"Leave.");
	add_dialog_choice(1,"Move the switch.");
	choice = run_dialog(1);
	if (choice == 1)
		end();
	
	// Into which position?
	reset_dialog();
	add_dialog_str(0,"Into which position do you want to set the switch?",0);
	if (get_flag(250,2) > 0)
		add_dialog_str(1,"The left position is labeled _LOW._ The center position is labeled _MEDIUM._ The right position is labeled _HIGH._",0);
	if (safety != 1)
		add_dialog_choice(0,"Move it to the left position.");
	else
		add_dialog_choice(0,"Leave it alone.");
	if (safety != 2)
		add_dialog_choice(1,"Move it to the center position.");
	else
		add_dialog_choice(1,"Leave it alone.");
	if (safety != 3)
		add_dialog_choice(2,"Move it to the right position.");
	else
		add_dialog_choice(2,"Leave it alone.");
	choice = run_dialog(1);
	
	// ***
	// If the choice was to leave the switch alone, end immediately.
	if (choice == safety)
		end();
	
	// If it didn't work, give a message and end. Remember, low SAFETY requires
	// medium or high AUTHORIZATION. 
	if ((authoriz == 1) && (choice == 1) && (safety != 1))
		{message_dialog("You move the switch to the left, but it flips right back to where it was! A voice speaks in your mind, saying, _Your Authorization level is not high enough for your choice._","");
		end();
		}
		
	// It worked, so set the variable.
	safety = choice;
	play_sound(94);
	message_dialog("You flip the switch into place. You hear a slight hum for a brief time, but nothing else happens.","");
break;

beginstate 23;
if (get_flag(14,15) != 0)
	end();
	
	reset_dialog();
	add_dialog_str(0,"When you step into this room, you see one of the most astonishing sights yet: a room filled with a whole array of crystals and machines, energy crackling everywhere, and demons running about handling the system.",0);
	add_dialog_str(1,"Surely there is something important going on here, if you have time to take a look. Unfortunately, the demons have stopped working on their respective jobs and have decided to take care of you first.",0);
	add_dialog_str(2,"The imps in this room look a bit strange. You can't quite articulate what it is about them that is peculiar, but something is a bit off.",0);
	set_flag(14,15,run_dialog(1));
break;

beginstate 24; // POWER state of crystal puzzle (for details see state 22)
	// Is puzzle active?
	if (get_flag(14,21))
		{message_dialog("You've fiddled with the control panels enough. You leave this one alone.","");
		end();
		}
	
	// Do they want to move the switch?
	reset_dialog();
	add_dialog_str(0,"You come to a control panel. It has a label and a switch with three settings.",0);
	if (get_flag(250,2) == 0)
		add_dialog_str(1,"The label has strange writing that you can't read, and above each of the three switches are more words that you can't read.",0);
	if (get_flag(250,2) == 1)
		add_dialog_str(1,"The label says _POWER._ Above each of the three settings are, from left to right, _LOW,_ _MEDIUM,_ and _HIGH._",0);
	if (get_flag(250,2) == 2)
		add_dialog_str(1,"The label says _POWER TO SYSTEM._ Above each of the three settings are, from right to left, _LOW,_ _MEDIUM,_ and _HIGH._",0);
	if (power == 1)
		add_dialog_str(2,"The switch is in the left position.",0);
	if (power == 2)
		add_dialog_str(2,"The switch is in the center position.",0);
	if (power == 3)
		add_dialog_str(2,"The switch is in the right position.",0);
	add_dialog_str(3,"What do you do?",0);
	add_dialog_choice(0,"Leave.");
	add_dialog_choice(1,"Move the switch.");
	choice = run_dialog(1);
	if (choice == 1)
		end();
	
	// Into which position?
	reset_dialog();
	add_dialog_str(0,"Into which position do you want to set the switch?",0);
	if (get_flag(250,2) > 0)
		add_dialog_str(1,"The left position is labeled _LOW._ The center position is labeled _MEDIUM._ The right position is labeled _HIGH._",0);
	if (power != 1)
		add_dialog_choice(0,"Move it to the left position.");
	else
		add_dialog_choice(0,"Leave it alone.");
	if (power != 2)
		add_dialog_choice(1,"Move it to the center position.");
	else
		add_dialog_choice(1,"Leave it alone.");
	if (power != 3)
		add_dialog_choice(2,"Move it to the right position.");
	else
		add_dialog_choice(2,"Leave it alone.");
	 choice = run_dialog(1);
	
	// ***
	// If the choice was to leave the switch alone, end immediately.
	if (choice == power)
		end();
		
	// ***
	// If it didn't work, give a message and end. Remember, medium POWER
	// requires medium or high AUTHORIZATION, and high POWER requires medium or
	// high AUTHORIZATION and low SAFETY.
	
	// AUTHORIZATION was too low.
	if ((choice >= 1) && (authoriz == 1))
		{message_dialog("You adjust the switch. However, as soon as you take your hand off of it, the panel glows red and the switch moves back into its previous position!","A voice speaks in your mind, saying, _Your Authorization level is not high enough for your choice._");
		end();
		}
	
	// SAFETY was too high.
	if ((choice == 3) && (safety > 1))
		{message_dialog("You adjust the switch. However, as soon as you take your hand off of it, the panel glows red and the switch moves back into its previous position!","A voice speaks in your mind, saying, _Your choice violates the current Safety level._");
		end();
		}
		
	// ***
	// The switch moved successfully. Set the variable and respond. Remember,
	// the solution is high ACCESS and medium POWER, and death comes from high
	// ACCESS and high POWER.
	
	power = choice;
	play_sound(94);
	
	// Setting POWER to low. This shouldn't be possible from high ACCESS.
	if (choice == 1)
		{if (access == 1) // low ACCESS
			message_dialog("You flip the switch into place. The humming and crackling that you had heard now stops.","");
		if (access == 2) // medium ACCESS
			message_dialog("You flip the switch into place. You could swear you almost heard a groan or a sigh.","");
		end();
		}
	
	// Setting POWER to medium.
	if (choice == 2)
		{if (access == 1) // low ACCESS
			message_dialog("You flip the switch into place. The whole room hums with energy, and the floor vibrates.","");
		if (access == 2) // medium ACCESS
			message_dialog("You flip the switch into place. You hear a slight hum for a brief time, and then the barriers around the crystals in the center of this room distort: they bend outwards, as if under tremendous pressure from the inside.","You begin to hear voices whispering in your mind. _Set us free,_ they say. _Set us free._");
		if (access == 3) // high ACCESS, so SOLVED
			set_state_continue(29);
		end();
		}
	
	// Setting POWER to high.
	if (choice == 3)
		{if (access == 1) // low ACCESS
			message_dialog("You flip the switch into place. You hear a slight hum for a brief time, and then the barriers around the crystals in the center of this room glow intensely. The air feels charged, as if it were about to explode. The feeling almost hurts.","Distantly, you can hear screams of pain, or at least you think you can. You may not want to keep this switch in this spot.");
		if (access == 2) // medium ACCESS
			message_dialog("You flip the switch into place. You hear a slight hum for a brief time, and then the barriers around the crystals in the center of this room glow intensely. The air feels charged, as if it were about to explode. The feeling almost hurts.","You can hear unnervingly vivid screams of pain nearby. _Turn it down!_ voices whisper sharply into your mind. _Turn the power down!_");
		if (access == 3) // high ACCESS, so DEATH
			set_state_continue(30);
		}
break;

beginstate 25; // SECURITY state of crystal puzzle (for details see state 22)
	// Is puzzle active?
	if (get_flag(14,21))
		{message_dialog("You've fiddled with the control panels enough. You leave this one alone.","");
		end();
		}
	
	// Do they want to move the switch?
	reset_dialog();
	add_dialog_str(0,"You come to a control panel. It has a label and a switch with three settings.",0);
	if (get_flag(250,2) == 0)
		add_dialog_str(1,"The label has strange writing that you can't read, and above each of the three switches are more words that you can't read.",0);
	if (get_flag(250,2) == 1)
		add_dialog_str(1,"The label says _SECURITY._ Above each of the three settings are, from left to right, _LOW,_ _MEDIUM,_ and _HIGH._",0);
	if (get_flag(250,2) == 2)
		add_dialog_str(1,"The label says _SECURITY LEVEL._ Above each of the three settings are, from right to left, _LOW,_ _MEDIUM,_ and _HIGH._",0);
	if (security == 1)
		add_dialog_str(2,"The switch is in the left position.",0);
	if (security == 2)
		add_dialog_str(2,"The switch is in the center position.",0);
	if (security == 3)
		add_dialog_str(2,"The switch is in the right position.",0);
	add_dialog_str(3,"What do you do?",0);
	add_dialog_choice(0,"Leave.");
	add_dialog_choice(1,"Move the switch.");
	choice = run_dialog(1);
	if (choice == 1)
		end();
	
	// Into which position?
	reset_dialog();
	add_dialog_str(0,"Into which position do you want to set the switch?",0);
	if (get_flag(250,2) > 0)
		add_dialog_str(1,"The left position is labeled _LOW._ The center position is labeled _MEDIUM._ The right position is labeled _HIGH._",0);
	if (security != 1)
		add_dialog_choice(0,"Move it to the left position.");
	else
		add_dialog_choice(0,"Leave it alone.");
	if (security != 2)
		add_dialog_choice(1,"Move it to the center position.");
	else
		add_dialog_choice(1,"Leave it alone.");
	if (security != 3)
		add_dialog_choice(2,"Move it to the right position.");
	else
		add_dialog_choice(2,"Leave it alone.");
	choice = run_dialog(1);
	
	// ***
	// If the choice was to leave the switch alone, end immediately.
	if (choice == security)
		end();
		
	// ***
	// If it didn't work, give a message and end. Remember, low SECURITY
	// requires medium or less SAFETY.
	
	if ((choice == 1) && (safety == 3))
		{message_dialog("You adjust the switch. However, as soon as you take your hand off of it, the panel glows red and the switch moves back into its previous position!","A voice speaks in your mind, saying, _Your choice violates the current Safety level._");
		end();
		}
		
	// ***
	// It worked, so set the variable.
	security = choice;
	play_sound(94);
	message_dialog("You flip the switch into place. You hear a slight hum for a brief time, but nothing else happens.","");
break;

beginstate 26; // AUTHORIZATION state of crystal puzzle (for details see state
			// 22)
	// Is puzzle active?
	if (get_flag(14,21))
		{message_dialog("You've fiddled with the control panels enough. You leave this one alone.","");
		end();
		}
	
	// Do they want to move the switch?
	reset_dialog();
	add_dialog_str(0,"You come to a control panel. It has a label and a switch with three settings.",0);
	if (get_flag(250,2) == 0)
		add_dialog_str(1,"The label has strange writing that you can't read, and above each of the three switches are more words that you can't read.",0);
	if (get_flag(250,2) == 1)
		add_dialog_str(1,"You can't read the label. Above each of the three settings are, from left to right, _LOW,_ _MEDIUM,_ and _HIGH._",0);
	if (get_flag(250,2) == 2)
		add_dialog_str(1,"The label says _AUTHORIZATION._ Above each of the three settings are, from right to left, _LOW,_ _MEDIUM,_ and _HIGH._",0);
	if (authoriz == 1)
		add_dialog_str(2,"The switch is in the left position.",0);
	if (authoriz == 2)
		add_dialog_str(2,"The switch is in the center position.",0);
	if (authoriz == 3)
		add_dialog_str(2,"The switch is in the right position.",0);
	add_dialog_str(3,"What do you do?",0);
	add_dialog_choice(0,"Leave.");
	add_dialog_choice(1,"Move the switch.");
	choice = run_dialog(1);
	if (choice == 1)
		end();
	
	// Into which position?
	reset_dialog();
	add_dialog_str(0,"Into which position do you want to set the switch?",0);
	if (get_flag(250,2) > 0)
		add_dialog_str(1,"The left position is labeled _LOW._ The center position is labeled _MEDIUM._ The right position is labeled _HIGH._",0);
	if (authoriz != 1)
		add_dialog_choice(0,"Move it to the left position.");
	else
		add_dialog_choice(0,"Leave it alone.");
	if (authoriz != 2)
		add_dialog_choice(1,"Move it to the center position.");
	else
		add_dialog_choice(1,"Leave it alone.");
	if (authoriz != 3)
		add_dialog_choice(2,"Move it to the right position.");
	else
		add_dialog_choice(2,"Leave it alone.");
	choice = run_dialog(1);
	
	// ***
	// If the choice was to leave the switch alone, end immediately.
	if (choice == authoriz)
		end();
	
	// If it didn't work, give a message and end. Remember, medium AUTHORIZATION
	// requires low SECURITY, and high AUTHORIZATION is impossible. Also, if
	// the party is trying for high AUTHORIZATION, give them a little zap.
	
	// SECURITY too high
	if ((choice == 2) && (security > 1))
		{message_dialog("A voice begins barking strange words at you. You look around quickly, expecting to see a large demon right behind you, but then you realize that the voice is only in your mind.","Unfortunately, you don't know how to respond. The spell senses this, apparently, because the presence vanishes. The switch moves back to its original position. A voice says in your mind, _Authorization level denied._");
		end();
		}
	
	// Requested AUTHORIZATION level too high.
	if (choice == 3)
		{message_dialog("A voice begins barking strange words at you. You look around quickly, expecting to see a large demon right behind you, but then you realize that the voice is only in your mind.","Unfortunately, you don't know how to respond. The spell senses this, apparently, because the presence vanishes. The switch moves back to its original position, and small bolts shoot from the panel, shocking you!");
		i = 0;
		while (char_ok(i) == 0)
			{i = i + 1; }
		put_boom_on_char(i,6,0);
		damage_char(i,100,3);
		message_dialog("A voice says in your mind, _Authorization level denied. Intruder suspected. This event will be reported to the next available haakai leader._","");
		end();
		}
	
	// ***
	// It worked, so set the variable.
	authoriz = choice;
	play_sound(94);
	message_dialog("You flip the switch into place. You hear a slight hum for a brief time, but nothing else happens.","");
break;

beginstate 27; // ACCESS state of crystal puzzle (for details see state 22)
	// Is puzzle active?
	if (get_flag(14,21))
		{message_dialog("You've fiddled with the control panels enough. You leave this one alone.","");
		end();
		}
	
	// Do they want to move the switch?
	reset_dialog();
	add_dialog_str(0,"You come to a control panel. It has a label and a switch with three settings.",0);
	if (get_flag(250,2) == 0)
		add_dialog_str(1,"The label has strange writing that you can't read, and above each of the three switches are more words that you can't read.",0);
	if (get_flag(250,2) == 1)
		add_dialog_str(1,"The label says _ACCESS._ Above each of the three settings are, from left to right, _LOW,_ _MEDIUM,_ and _HIGH._",0);
	if (get_flag(250,2) == 2)
		add_dialog_str(1,"The label says _ACCESS TO SLITH SOULS._ Above each of the three settings are, from right to left, _LOW,_ _MEDIUM,_ and _HIGH._",0);
	if (access == 1)
		add_dialog_str(2,"The switch is in the left position.",0);
	if (access == 2)
		add_dialog_str(2,"The switch is in the center position.",0);
	if (access == 3)
		add_dialog_str(2,"The switch is in the right position.",0);
	if (power == 1)
		add_dialog_str(3,"There is a space for a small light in the corner of this panel. It is off, however.",0);
	add_dialog_str(4,"What do you do?",0);
	add_dialog_choice(0,"Leave.");
	add_dialog_choice(1,"Move the switch.");
	choice = run_dialog(1);
	if (choice == 1)
		end();
	
	// Into which position?
	i = access;
	reset_dialog();
	add_dialog_str(0,"Into which position do you want to set the switch?",0);
	if (get_flag(250,2) > 0)
		add_dialog_str(1,"The left position is labeled _LOW._ The center position is labeled _MEDIUM._ The right position is labeled _HIGH._",0);
	if (access != 1)
		add_dialog_choice(0,"Move it to the left position.");
	else
		add_dialog_choice(0,"Leave it alone.");
	if (access != 2)
		add_dialog_choice(1,"Move it to the center position.");
	else
		add_dialog_choice(1,"Leave it alone.");
	if (access != 3)
		add_dialog_choice(2,"Move it to the right position.");
	else
		add_dialog_choice(2,"Leave it alone.");
	choice = run_dialog(1);
	
	// ***
	// If the choice was to leave the switch alone, end immediately.
	if (choice == access)
		end();
	
	// ***
	// If it didn't work, give a message and end. Remember, medium ACCESS
	// requires medium or low SAFETY, and high ACCESS requires medium or high
	// AUTHORIZATION and low SAFETY
	
	// SAFETY too high
	if (((choice == 2) && (safety == 3)) || ((choice == 3) && (safety >= 2)))
		{message_dialog("You adjust the switch. However, as soon as you take your hand off of it, the panel glows red and the switch moves back into its previous position!","A voice speaks in your mind, saying, _Your choice violates the current Safety level._");
		end();
		}

	// AUTHORIZATION too low
	if ((choice == 3) && (authoriz == 1))
		{message_dialog("You adjust the switch. However, as soon as you take your hand off of it, the panel glows red and the switch moves back into its previous position!","A voice speaks in your mind, saying, _Your Authorization level is not high enough for your choice._");
		end();
		}
	
	// ***
	// It worked, so set the variable. Respond accordingly.
	access = choice;
	play_sound(94);
	
	// If POWER is low, nothing happens.
	if (power == 1)
		{message_dialog("You flip the switch into place. You hear a slight hum for a brief time, but nothing else happens.","");
		end();
		}
		
	// If ACCESS is low
	if (choice == 1)
		{if (power == 2) // POWER is medium.
			message_dialog("You flip the switch into place. You could swear you almost heard a groan or a sigh.","");
		if (power == 3) // POWER is high.
			message_dialog("You flip the switch into place. The screams you thought you heard now cut out, silenced.","You suspect you haven't helped the problem.");
		end();
		}

	// If POWER is medium
	if (power == 2)
		{if (choice == 2) // ACCESS is medium. Suggest more.
			{message_dialog("You flip the switch into place. You hear a slight hum for a brief time, and then the barriers around the crystals in the center of this room distort: they bend outwards, as if under tremendous pressure from the inside.","You begin to hear voices whispering in your mind. _Set us free,_ they say. _Set us free._");
			end();
			}
		if (choice == 3) // ACCESS is high, so puzzle is SOLVED.
			set_state_continue(29);
		}
	
	// If POWER is high
	if (power == 3)
		{if (choice == 2) // ACCESS is medium. Warn.
			{message_dialog("You flip the switch into place. You hear a slight hum for a brief time, and the barriers around the crystals in the center of this room distort: they bend outwards, as if under tremendous pressure from the inside.","You begin to hear unnervingly vivid screams of pain nearby. _Turn it down!_ voices whisper sharply into your mind. _Turn the power down! And set us free!_");
			end();
			}
		if (choice == 3) // ACCESS is high, so DEATH.
			set_state_continue(30);
		}
break;

beginstate 28;
	reset_dialog();
	add_dialog_str(0,"On the wall here is a diagram. It appears to be of the room, describing the machinery at work! You took a closer look.",0);
	if (get_flag(250,2) == 0)
		add_dialog_str(1,"Unfortunately, you can't read a word of it. It is in a strange cursive style that is too different from the normal writing systems of Avernum.",0);
	else
		add_dialog_str(1,"The technical notes along the side are too complicated for you to read, but you can understand some of the words on the map itself.",0);
	if (get_flag(250,2) == 1)
		add_dialog_str(2,"Clockwise from the southwest platform, the devices read: _ACCESS,_ something, _SECURITY,_ _POWER,_ and something else. Well, there are many other words, too, but you don't know what they are.",0);
	if (get_flag(250,2) == 2)
		add_dialog_str(2,"Clockwise from the southwest platform, the devices read: _ACCESS TO SLITH SOULS,_ _AUTHORIZATION,_ _SECURITY LEVEL,_ _POWER TO SYSTEM,_ and _SAFETY OVERRIDE._ There are a few other words, too, but you can't read them.",0);
	dummy = run_dialog(1);
break;

beginstate 29;
	message_dialog("As soon as you move this switch into position, the room begins shaking. You grab the switch and try to move it again into a different position to stop whatever you started, but it's too late. Whatever you've done is done. Now you can only wait it out.","");
	
	set_total_visibility(1);
	force_view_center(20,23);
	force_instant_terrain_redraw();
	
	put_effect_on_space(20,22,6,1,2);
	put_effect_on_space(21,22,6,1,2);
	put_effect_on_space(20,23,6,1,2);
	put_effect_on_space(21,23,6,1,2);
	run_animation_sound(28);
	
	put_effect_on_space(20,22,6,1,2);
	put_effect_on_space(21,22,6,1,2);
	put_effect_on_space(20,23,6,1,2);
	put_effect_on_space(21,23,6,1,2);
	run_animation_sound(28);
	
	put_boom_on_space(20,22,1,0);
	put_boom_on_space(21,22,1,0);
	put_boom_on_space(20,23,1,0);
	put_boom_on_space(21,23,1,0);
	run_animation_sound(51);
	
	set_terrain(20,25,0);
	set_terrain(21,25,0);
	put_effect_on_space(20,25,8,1,2);
	put_effect_on_space(21,25,8,1,2);
	run_animation_sound(60);
	
	set_terrain(20,22,461);
	set_terrain(21,22,461);
	set_terrain(20,23,461);
	set_terrain(21,23,461);
	put_boom_on_space(20,22,1,0);
	put_boom_on_space(21,22,1,0);
	put_boom_on_space(20,23,1,0);
	put_boom_on_space(21,23,1,0);
	run_animation_sound(51);
	
	place_monster(21,24,254,0);
	set_character_facing(86,4);
	place_monster(20,24,254,0);
	set_character_facing(87,4);
	place_monster(19,23,249,0);
	set_character_facing(88,4);
	place_monster(19,22,249,0);
	set_character_facing(89,4);
	place_monster(22,22,249,0);
	set_character_facing(90,4);
	place_monster(22,23,249,0);
	set_character_facing(91,4);
	place_monster(22,21,240,0);
	set_character_facing(92,4);
	place_monster(21,21,240,0);
	set_character_facing(93,4);
	place_monster(20,21,240,0);
	set_character_facing(94,4);
	place_monster(19,21,240,0);
	set_character_facing(95,4);
	force_instant_terrain_redraw();
			
	message_dialog("Suddenly, a horde of slith spirits begin pouring out of the crystals! Watching them, you realize that must've been what this machinery was for: to imprison slith ghosts, presumably the ones that were not being used at the time.","The machinery must've been designed to let out a few at a time, but you managed to find a setting that opened the whole thing. The question remains open about what they are going to do, though.");
	i = 0;
	while (char_ok(i) == 0)
		{i = i + 1; }
	pause(1 * get_flag(250,8));
	
	j = 86;
	
	if (char_loc_y(i) < 22)
		set_character_facing(j,0);
	else
		set_character_facing(j,2);
		
	if (party_size() > 1)
		text_bubble_on_char(j,"Adventurers!");
	if (party_size() == 1)
		text_bubble_on_char(j,"Adventurer!");
	force_instant_terrain_redraw();
	pause(5 * get_flag(250,8));
	
	message_dialog("The word rings in your mind. This spirit is using magic to communicate with you.","");

	pause(1 * get_flag(250,8));
	
	text_bubble_on_char(j,"");
	text_bubble_on_char(j,"You have freed us!");
	force_instant_terrain_redraw();
	pause(6 * get_flag(250,8));
	
	text_bubble_on_char(j,"");
	text_bubble_on_char(j,"This is a great service,");
	force_instant_terrain_redraw();
	pause(6 * get_flag(250,8));
	
	text_bubble_on_char(j,"");
	text_bubble_on_char(j,"greater than any that an outsider");
	force_instant_terrain_redraw();
	pause(6 * get_flag(250,8));
	
	text_bubble_on_char(j,"");
	text_bubble_on_char(j,"has ever done for Bahssikava.");
	force_instant_terrain_redraw();
	pause(6 * get_flag(250,8));
	
	text_bubble_on_char(j,"");
	text_bubble_on_char(j,"However, the work is not finished.");
	force_instant_terrain_redraw();
	pause(6 * get_flag(250,8));
	
	text_bubble_on_char(j,"");
	text_bubble_on_char(j,"The foul demon Nolagh-Khar");
	force_instant_terrain_redraw();
	pause(6 * get_flag(250,8));
	
	text_bubble_on_char(j,"");
	text_bubble_on_char(j,"still reigns to the north");
	force_instant_terrain_redraw();
	pause(6 * get_flag(250,8));
	
	text_bubble_on_char(j,"");
	text_bubble_on_char(j,"in his stronghold, Khar-Grav.");
	force_instant_terrain_redraw();
	pause(6 * get_flag(250,8));
	
	text_bubble_on_char(j,"");
	text_bubble_on_char(j,"Help us.");
	force_instant_terrain_redraw();
	pause(6 * get_flag(250,8));
	
	text_bubble_on_char(j,"");
	text_bubble_on_char(j,"Kill Nolagh-Khar.");
	force_instant_terrain_redraw();
	pause(6 * get_flag(250,8));
	
	text_bubble_on_char(j,"");
	text_bubble_on_char(j,"We can offer you aid in your efforts.");
	force_instant_terrain_redraw();
	pause(6 * get_flag(250,8));
	
	add_dialog_str(0,"The ghost places special emphasis on the next words, making sure that you understand.",0);
	if (get_flag(14,29) == 0)
		{clear_buffer();
		append_string("_There are living sliths here in Bahssikava, in a cell to the southeast. They are from a town near here. They will help you. ");
		if (has_special_item(4) == 0)
			append_string(" The key to unlock their cells is on the jailer, who is east of here._");
		else
			append_string(" You already have the key to unlock their cells._");
		get_buffer_text(dlgstr);
		add_dialog_str(1,dlgstr,0);
		}
	add_dialog_str(2,"_We know that Nolagh-Khar relies on crystals for his monstrous power. We know that you must disrupt the energy flowing into him, or else you will not stand a chance in combat with him. We do not know how to do this, but it must be done._",0);
	add_dialog_str(3,"_There is a powerful weapon through the door to the north. We will unlock the door for you._",0);
	add_dialog_str(4,"_Finally, we will help you when you fight Nolagh-Khar. When you arrive in his throne room, we will be with you._",0);
	if (get_flag(214,0) == 0)
		add_dialog_str(5,"_You may be able to gain the help of the imps, too; I know that they are chafing under their masters as well._",0);
	
	text_bubble_on_char(j,"");
	force_instant_terrain_redraw();
	
	dummy = run_dialog(1);
	
	text_bubble_on_char(j,"We thank you for what you have done.");
	force_instant_terrain_redraw();
	pause(6 * get_flag(250,8));
	
	text_bubble_on_char(j,"");
	text_bubble_on_char(j,"Free us from our slavery,");
	force_instant_terrain_redraw();
	pause(6 * get_flag(250,8));
	
	text_bubble_on_char(j,"");
	text_bubble_on_char(j,"and we will be able to do more");
	force_instant_terrain_redraw();
	pause(6 * get_flag(250,8));

	text_bubble_on_char(j,"");
	text_bubble_on_char(j,"to show our thanks.");
	force_instant_terrain_redraw();
	pause(6 * get_flag(250,8));
	
	text_bubble_on_char(j,"");
	text_bubble_on_char(j,"Now, before we are caught,");
	force_instant_terrain_redraw();
	pause(6 * get_flag(250,8));
	
	text_bubble_on_char(j,"");
	text_bubble_on_char(j,"we must go.");
	force_instant_terrain_redraw();
	pause(6 * get_flag(250,8));
	
	text_bubble_on_char(j,"");
	force_instant_terrain_redraw();
	pause(1 * get_flag(250,8));
	
	put_effect_on_space(17,13,9,1,2);
	run_animation_sound(162);
	
	i = j;
	while (i <= 95)
		{erase_char(i);
		put_boom_on_char(i,2,0);
		i = i + 1; }
	run_animation_sound(163);
	
	message_dialog("And they are gone. The spirits vanished just like that. The energy in the room subsides, and the feeling of magic in the air is gone.","Living sliths in Bahssikava? This place may be more important than it at first seemed. Living sliths could give you real answers about the homeland, because the city has been connected to it for decades now.");
	
	i = 0;
	while (char_ok(i) == 0)
		{i = i + 1; }
	force_view_center(char_loc_x(i),char_loc_y(i));
	force_instant_terrain_redraw();
	
	set_total_visibility(0);
	set_flag(14,21,1);
	
break;

beginstate 30;
	play_sound(94);
	message_dialog("The power in the crystals in the center of the room surges out of control, and the crystals glow red and then white with heat. Then they explode!","");

	put_boom_on_space(20,22,1,0);
	put_boom_on_space(21,22,1,0);
	put_boom_on_space(20,23,1,0);
	put_boom_on_space(21,23,1,0);
	run_animation_sound(51);
	damage_char(1000,50,1);
	
	put_boom_on_space(20,22,1,0);
	put_boom_on_space(21,22,1,0);
	put_boom_on_space(20,23,1,0);
	put_boom_on_space(21,23,1,0);
	run_animation_sound(51);
	damage_char(1000,150,1);
	
	put_boom_on_space(20,22,1,0);
	put_boom_on_space(21,22,1,0);
	put_boom_on_space(20,23,1,0);
	put_boom_on_space(21,23,1,0);
	run_animation_sound(51);
	damage_char(1000,300,1);
	
	message_dialog("The room fills with fire, searing your skin, your eyes, and your lungs. You quickly succumb to the inevitable.","You probably shouldn't have tampered with the safety mechanism.");
	if (char_ok(5))
		kill_char(5,2,0);
	if (char_ok(0))
		kill_char(0,2,0);
	if (char_ok(1))
		kill_char(1,2,0);
	if (char_ok(2))
		kill_char(2,2,0);
	if (char_ok(3))
		kill_char(3,2,0);
break;

beginstate 31;
if (get_flag(14,22) != 0)
	end();
	
	message_dialog("The moment that you step into this chamber, a feeling of powerful holiness washes over you. It was a temple in Ancient Bahssikava, and the demons have not been able to enter and profane it.","");
	set_flag(14,22,1);
break;

beginstate 32;
if (get_flag(14,23) != 0)
	end();
	
	reset_dialog();
	add_dialog_str(0,"Along the west wall are many elaborate paintings, beautiful ones that appear to tell stories. You can't quite follow any of them, but they look carefully crafted and intricate.",0);
	if (get_flag(250,0) == 4)
		add_dialog_str(1,"You look over the paintings, your slith eyes taking in every detail. This wall by itself would be enough to redeem your race in the eyes of all who live in Avernum, if its contents were known. Once again, your very soul resonates with what you see.",0);
	else
		add_dialog_str(1,"Again you are forcibly reminded how much your discoveries here in Bahssikava could change the popular perception of sliths. No stupid race could conceive of stories this detailed.",0);
	set_flag(14,23,run_dialog(1));
break;

beginstate 33;
if (get_flag(14,24) != 0)
	end();
	
	reset_dialog();
	add_dialog_str(0,"As you near this altar, you feel a great magical presence searching you, checking you. Not knowing what else to do, you stop and let it.",0);
	if (get_flag(14,25) == 0)
		{add_dialog_str(1,"It disapproves! You feel yourself unable to continue walking towards the altar. Knowing slith customs, you probably have to wash your hands in one of the pools to the south.",0);
		dummy = run_dialog(1);
		block_entry(1);
		end();
		}
	add_dialog_str(1,"It approves. You approach the altar, and as you do, you hear a soft, sibilant voice in your mind.",0);
	add_dialog_str(2,"_To defeat the demons, you will need a suitable weapon._",0);
	set_flag(14,24,run_dialog(1));
	pause(1 * get_flag(250,8));
	
	put_item_on_spot(17,4,452);
	put_effect_on_space(17,4,4,1,2);
	run_animation_sound(62);
break;

beginstate 34;
	reset_dialog();
	add_dialog_str(0,"There is a small, humble pool of water here. What do you do?",0);
	add_dialog_choice(0,"Leave.");
	add_dialog_choice(1,"Wash your hands in it.");
	add_dialog_choice(2,"Drink some of the water.");
	choice = run_dialog(1);
	if (choice == 1)
		end();
	if (choice == 2)
		{message_dialog("You wash your hands in the cool water. Nothing that you can see happens, though.","");
		set_flag(14,25,1); }
	if (choice == 3)
		message_dialog("You drink some of the cool water. It tastes slightly bitter. Nothing you can see happens, though.","");
break;

beginstate 35;
if (get_flag(14,1) == 1)
	set_flag(14,1,0);
break;

beginstate 36;
if (get_flag(14,26) != 0)
	end();
	
	message_dialog("Judging by appearances, this was once the home of a Bahssikavan family. It has since been ransacked and badly damaged by demons.","");
	set_flag(14,26,1);
break;

beginstate 37;
if (get_flag(14,27) != 0)
	end();
	
	clear_buffer();
	if (get_flag(12,8) == 1)
		append_string("A little mental geometry allows you to figure out that it probably led to the school, or at least near there.");
	get_buffer_text(dlgstr);
	message_dialog("As is so frequently the case in Bahssikava, there was once a passageway through here, but it has been destroyed by a cave-in.",dlgstr);
	set_flag(14,27,1);
break;

beginstate 38;
if (get_flag(14,28) != 0)
	end();
	
	message_dialog("There is a small beach here, which presumably the Bahssikavans used for fishing in the river. One of their boats is still here, floating in the water, pressed against some rocks that appear to have fallen from the ceiling at some point.","");
	set_flag(14,28,1);
break;

beginstate 39;
	set_flag(14,1,2);
break;

beginstate 40;
if (has_special_item(4) > 0)
	end();
	
	reset_dialog_preset_options(1);
	add_dialog_str(0,"Inside this box, there is a small, copper key. What do you do?",0);
	choice = run_dialog(1);
	if (choice == 2)
		change_spec_item(4,1);
break;

beginstate 41;
	if (get_flag(214,0) == 0)
		message_dialog("This is the dead body of an imp. It was savagely beaten to death and then mutilated. If this is the standard treatment of imps around here, they must truly hate their captors, the demons.","This may be useful information.");
	else
		message_dialog("This is the dead body of an imp. It was savagely beaten to death and then mutilated. If this is the standard treatment of imps around here, they must truly hate their captors, the demons.","This explains their reaction when given a chance to escape.");
break;

beginstate 42;
if (get_flag(214,1) == 0)
	{message_dialog("You can hear loud clanking near here. It sounds as though a bunch of creatures are at work in a mine just farther down this passage, and if you are not mistaken, several demons are barking orders, too.","It may be dangerous to continue.");
	set_flag(214,1,1); }
	
if (get_flag(14,12) == 2)
	{message_dialog("Suddenly you hear something behind you, and you spin, ready to fight. It is an imp, but it doesn't look hostile. Then you realize it is Boarag, the imp whom you agreed to help.","_The mine is to the west and north,_ Boarag says. _Enter it. Do not worry. I will be right behind you, and all of the imps will join in to help you fight off the demons._ He disappears into the shadows again.");
	set_flag(14,12,3);
	}
break;

beginstate 43;
	inc_flag(214,2,1);
	if (get_flag(14,12) < 2)
		end();
		
	if (get_flag(214,2) == 1)
		{message_dialog("As this demon dies, Boarag rushes in from the north! _Fight!_ he roars to the other imps. _Fight your captors! Fight for freedom!_ He charges into battle.","");
		relocate_character(37,6,15);
		set_character_facing(37,4);
		force_instant_terrain_redraw(); }
		
	if (get_flag(214,2) == 3)
		{i = get_ran(1,74,80);
		j = get_ran(1,74,80);
		k = get_ran(1,74,80);
		
		text_bubble_on_char(i,"Yay!");
		force_instant_terrain_redraw();
		pause(1 * get_flag(250,8));
		
		text_bubble_on_char(j,"Take that!");
		force_instant_terrain_redraw();
		pause(1 * get_flag(250,8));
		
		text_bubble_on_char(k,"*roars*");
		force_instant_terrain_redraw();
		pause(5 * get_flag(250,8));
		
		text_bubble_on_char(i,"");
		text_bubble_on_char(j,"");
		text_bubble_on_char(k,"");
		force_instant_terrain_redraw();
		pause(1 * get_flag(250,8));
		
		message_dialog("As the third demon dies, there is scattered cheering among the imps. They seem to realize now that they just might win this.","");
		}
		
	if (get_flag(214,2) != 4)
		end();
		
	if (char_ok(81))
		k = 81;
	if (char_ok(82))
		k = 82;
	if (char_ok(83))
		k = 83;
	if (char_ok(85))
		k = 85;
	
	i = 0;
	if (char_ok(37))
		i = 37;
	else
		{j = 74;
		while ((j <= 84) && (i == 0))
			{if (char_ok(j))
				i = j;
			j = j + 1;
			}
		}
	if (i == 0)
		{message_dialog("You strike down the last demon, and as you do, you glance around.","You agreed to help the imps, but they all died in the battle. The blood is everywhere. You stare, looking around in horror at your complete and total failure. You will have to defeat Nolagh-Khar without their help.");
		end(); }
		
	text_bubble_on_char(i,"Aaaaaaaaahhh!");
	force_instant_terrain_redraw();
	pause(1 * get_flag(250,8));
	
	put_jagged_zap(char_loc_x(i),char_loc_y(i),char_loc_x(k),char_loc_y(k),6);
	put_boom_on_char(k,6,0);
	run_animation_sound(65);
	kill_char(k,2,0);
	text_bubble_on_char(i,"");
	force_instant_terrain_redraw();
	pause(1 * get_flag(250,8));
	
	text_bubble_on_char(i,"We are free!");
	force_instant_terrain_redraw();
	pause(6 * get_flag(250,8));
	
	text_bubble_on_char(i,"");
	force_instant_terrain_redraw();
	pause(1 * get_flag(250,8));
	
	if (i == 84)
		{message_dialog("The one surviving imp looks up at you. _You have defeated the demons and liberated me from bondage. You are going to kill Nolagh-Khar, aren't you?_ You nod.","_When you fight him, I will make sure that every imp in Bahssikava joins on your side. Hail!_ With that, he hurries off.");
		set_flag(214,0,1);
		erase_char(i);
		end();
		}
	
	reset_dialog();
	if (char_ok(37))
		add_dialog_str(0,"The remaining imps turn and look at you. Boarag speaks.",0);
	else
		add_dialog_str(0,"The remaining imps turn and look at you. One of them, their apparent leader in Boarag's absence, speaks.",0);
	add_dialog_str(1,"_You have saved us. We owe you everything that we have. Unfortunately, because of the demons, we have nothing right now, no way to reward you. We must ask more of you: slay Nolagh-Khar. Only if you do that will we be able to reward you as you deserve._",0);
	if (party_size() > 1)
		add_dialog_str(2,"The imp turns to the others. _And we will be there to fight Nolagh-Khar along with them, won't we? We will help them free us from our slavery and defeat the demon lord!_ They cheer an affirmative.",0);
	else
		add_dialog_str(2,"The imp turns to the others. _And we will be there to fight Nolagh-Khar along with our liberator, won't we? We will help this one free us from our slavery and defeat the demon lord!_ They cheer an affirmative.",0);
	add_dialog_str(3,"He turns back to you. _We must travel by secret ways not to raise an alarm, but we will join you when you reach the dark master.",0);
	set_flag(214,0,run_dialog(1));
	erase_char(37);
	erase_char(74);
	erase_char(75);
	erase_char(76);
	erase_char(77);
	erase_char(78);
	erase_char(79);
	erase_char(80);
	erase_char(84);
	
break;

beginstate 44;
	block_entry(1);
	message_dialog("As you round this corner, you hear the sounds of many, many more miners than were in this mine, and many more demon guards. Although you could defeat the ones in this mine, you'd be far outnumbered if you continued.","Wisely, you decide to turn back.");
break;

beginstate 45;
if (get_flag(250,4) == 1) // Phaedra dead
	{add_dialog_str(0,"There is a small, dark passage here. However, you can't go this way: Phaedra needs you to bring her back to the guardpost.",0);
	dummy = run_dialog(1);
	block_entry(1);
	end();
	}

	reset_dialog();
	add_dialog_str(0,"There is a small, dark passage here. Do you wish to walk down it?",0);
	add_dialog_choice(0,"Leave.");
	add_dialog_choice(1,"Walk down the passage.");
	choice = run_dialog(1);
	if (choice == 1)
		block_entry(1);
	if (choice == 2)
		move_to_new_town(15,58,57);
break;

beginstate 46;
if ((get_flag(214,3) == 0) && (char_ok(73)))
	{set_flag(214,3,1);
	message_dialog("The sound of talking or growling (or whatever) is directly west of you now. The wall must be extremely thin for you to be able to hear it from here.","");
	}
break;

beginstate 47;
if ((get_flag(214,4) == 0) && (char_ok(73)))
	{set_flag(214,4,1);
	message_dialog("You hear a distant sound that is somewhat peculiar. It sounds almost like talking, but it cannot possibly be. It is too low, harsh, and violent. It sounds like growling, except it has regular patterns and rhythms like language.","Whatever it is, it is coming from the southeast.");
	}
break;

beginstate 48;
if (get_flag(214,5) == 0)
	{set_flag(214,5,1);
	message_dialog("This room is filled with huge raw crystals. They have not been powered or shaped or enchanted in any way; presumably that will be done later. Right now they are just being stored.","");
	}
break;